package com.example1.polaris;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ClientList extends ListActivity {
	 ArrayList<HashMap<String, String>> ListProd;
	 JSONArray orale=null;
	 String Id_Cliente;
	 String NombreC;

	
	@SuppressLint("ShowToast")
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aluser);
		ListProd = new ArrayList<HashMap<String, String>>();

		  
		  ListView lv= getListView();
		  lv.setOnItemClickListener(new OnItemClickListener() {
			 

			@Override
			 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // getting values from selected ListItem
				//String hi =(TextView) view.findViewById(R.id.pid).getText().toString();
				String hi=view.findViewById(R.id.ppp).getContext().toString();
           	Toast.makeText(getApplicationContext(), "Id_Cliente"+hi, 1000).show();
               Intent in = new Intent(getApplicationContext(),Producto.class);
               in.putExtra("Id_Cliente",hi);
               startActivityForResult(in,100);
				// TODO Auto-generated method stub
				
			}});
	      				
					// TODO Auto-generated method stub					     
	   	     try {
				String data = null;
			data=httpGetData("http://10.0.2.2/sistemapolaris/datosClientes.php");
				if(data.length()>1)
				orale=new JSONArray(data);
					    //nombre.setText(ja.toString());
							
				for (int i = 0; i < orale.length(); i++) {	
			    JSONObject c = orale.getJSONObject(i);
			                        // Storing each json item in variable
			                       Id_Cliente = c.getString("Id_Cliente");
			                       NombreC = c.getString("NombreC");
			                      
			                      // nombre.setText(id.toString());
			                       HashMap<String, String> map = new HashMap<String, String>();
			                       
			                        // adding each child node to HashMap key => value
			                       
			                        map.put("Id_Cliente",Id_Cliente);
			                        map.put("NombreC",NombreC);
		
			                        // adding HashList to ArrayList
			                        ListProd.add(map);
								}
							ListAdapter adapter = new SimpleAdapter(ClientList.this,ListProd,R.layout.listuser, new String[] {"Id_Cliente","NombreC"},
		                            new int[] { R.id.ppp, R.id.userq});
		                    // updating listview
		                    setListAdapter(adapter);		
						} 
						catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							Toast.makeText(getApplicationContext(), "Error  del servidor, verifique su conexion a internet y vuelva a intentarlo.", 1000).show();
							//nombre.setText(ja.toString());
						}       
	    }
	    

	    public String httpGetData(String mURL) {
	           String response="";
	           mURL=mURL.replace(" ", "%20");
	            Log.i("LocAndroid Response HTTP Threas","Ejecutando get 0: "+mURL);
	             HttpClient httpclient = new DefaultHttpClient();
	             
	            Log.i("LocAndroid Response HTTP Thread","Ejecutando get 1");
	       	HttpGet httppost = new HttpGet(mURL);
	            Log.i("LocAndroid Response HTTP Thread","Ejecutando get 2");
	   try {
	      

	            Log.i("LocAndroid Response HTTP","Ejecutando get");
	           // Execute HTTP Post Request
	         ResponseHandler<String> responseHandler=new BasicResponseHandler();
	           	response = httpclient.execute(httppost,responseHandler);
	            Log.i("LocAndroid Response HTTP",response);
	       	} catch (ClientProtocolException e) {
	           Log.i("LocAndroid Response HTTP ERROR 1",e.getMessage());
	           // TODO Auto-generated catch block
	       } catch (IOException e) {
	           
	           Log.i("LocAndroid Response HTTP ERROR 2",e.getMessage());
	           // TODO Auto-generated catch block
	       }
	       return response;
	       
	       }  
	   
	}
 
